package com.eystar.pdc.controller;

import com.alibaba.fastjson.JSONArray;
import com.eystar.ext.route.ControllerBind;
import com.eystar.pdc.aop.auth.AuthAnno;
import com.eystar.pdc.aop.auth.AuthAnno.AuthType;
import com.eystar.pdc.aop.log.LogAnno;
import com.eystar.pdc.aop.log.LogAnno.LogType;
import com.eystar.pdc.model.Org;

/**
 * 机构管理
 * @author Administrator
 *
 */
@ControllerBind(controllerKey="/org", viewPath="/pdc")
@AuthAnno(type = AuthType.Super, res = "org")
public class OrgController extends BaseController {

	/**
	 * 列表展示页面
	 * @throws Exception
	 */
	@AuthAnno(auth = "look")
	@LogAnno(desc = "查看机构列表", type = LogType.SAVE)
	public void index() throws Exception{
		render("org_list.html");
	}

	/**
	 * 树形结构展示
	 * @throws Exception
	 */
	public void tree() throws Exception{
		JSONArray array = Org.dao.listOrgJson(getModel(Org.class));
		renderJson(array);
	}
	
	/**
	 * 跳转到添加页面
	 * @throws Exception
	 */
	@AuthAnno(auth = "save")
	public void add() throws Exception{
		render("org_form.html");
	}
	
	/**
	 * 点击保存按钮
	 * @throws Exception
	 */
	@AuthAnno(auth = "save")
	@LogAnno(desc = "添加机构${org.org_name}", type = LogType.SAVE)
	public void save() throws Exception{
		Org org = null;
		try {
			org = getSaveModel(Org.class);
			org.save();
			renderJson(ok());
		} catch (Exception e) {
			logger.error("保存出错！" + org.toJson(), e);
			renderJson(fail());
		}
	}
	
	/**
	 * 跳转到修改页面
	 * @throws Exception
	 */
	@AuthAnno(auth = "update")
	public void edit() throws Exception{
		setAttr("org", Org.dao.findOrgById(getPara()));
		render("org_form.html");
	}
	
	/**
	 * 点击修改按钮
	 * @throws Exception
	 */
	@AuthAnno(auth = "update")
	@LogAnno(desc = "修改机构${role.role_name}", type = LogType.UPDATE)
	public void update() throws Exception{
		Org org = null;
		try {
			org = getUpdateModel(Org.class);
			org.update();
			renderJson(ok());
		} catch (Exception e) {
			logger.error("修改出错！" + org.toJson(), e);
			renderJson(fail());
		}
		
	}
	
	/**
	 * 删除方法
	 */
	@AuthAnno(auth = "delete")
	@LogAnno(desc = "删除机构", type = LogType.DELETE)
	public void delete() throws Exception {
		Org.dao.deleteByIds(getPara("ids"));
		renderJson(ok());
	}
	
}